//
//  TemplateFoundDetailsHeaderView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/17.
//

#import "TemplateFoundDetailsHeaderView.h"

@interface TemplateFoundDetailsHeaderView ()<UITextFieldDelegate>
///默认的任务
@property (nonatomic ,strong) UILabel * numberLab;

@property (nonatomic ,strong) UILabel * nameLab;

///删除按钮
@property (nonatomic ,strong) UIButton * removeBtn;

@property (nonatomic ,strong) UILabel * sourceFlagLab;

//收缩的按钮
@property (nonatomic ,strong) UIButton * sxBtn;
///起止天数
@property (nonatomic ,strong) UIView * qztsView;
///开始天数
@property (nonatomic ,strong) UITextField * startDay;
///结束天数
@property (nonatomic ,strong) UITextField * endDay;
///任务频次
@property (nonatomic ,strong) UIView * rwpcView;
///任务频次
@property (nonatomic ,strong) UILabel * rateLab;

///任务时间
@property (nonatomic ,strong) UIView * rwsjView;
///放时间的view
@property (nonatomic ,strong) UIView * rwTimeView;
@end

@implementation TemplateFoundDetailsHeaderView


+ (instancetype)NewView {
    TemplateFoundDetailsHeaderView * view = [TemplateFoundDetailsHeaderView new];
    view.clipsToBounds = YES;
    view.backgroundColor = UIColor.whiteColor;
    [view newUI];
    return view;
}
- (void)newUI{
    MJWeakSelf
    UILabel * numberLab = [DTKTool labelWithText:@"1" andFont:kCFont(12) andTextColor:UIColor.whiteColor];
    numberLab.textAlignment = NSTextAlignmentCenter;
    numberLab.backgroundColor = KcolorHex(@"#2C72F7");
    self.numberLab = numberLab;
    [DTKTool YJBK:kwt(10) andView:numberLab andWidth:kwt(2) andColor:KcolorHex(@"#2C72F74c")];
    [self addSubview:numberLab];
    [numberLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(20));
        make.top.left.offset(kwt(13));
    }];
    
    
    UILabel * nameLab = [DTKTool labelWithText:@"-" andFont:kCFont(17) andTextColor:KcolorHex(@"#2C72F7")];
    self.nameLab = nameLab;
    [self addSubview:nameLab];
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(numberLab);
        make.left.offset(kwt(40));
    }];
    
    UIButton * sxBtn = [UIButton buttonWithType:0];
    self.sxBtn = sxBtn;
    sxBtn.addTarget(self,@selector(openSXBtnClick));
    [sxBtn setImage:[UIImage imageNamed:@"template_expand"] forState:UIControlStateNormal];
    [sxBtn setImage:[UIImage imageNamed:@"template_retract"] forState:UIControlStateSelected];
    [self addSubview:sxBtn];
    [sxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(numberLab);
        make.width.height.offset(kwt(20));
        make.right.offset(kwt(-14));
    }];
    
    UIButton * removeBtn = [UIButton buttonWithType:0];
    self.removeBtn = removeBtn;
    removeBtn.addTarget(self,@selector(scbtnClick));
    [removeBtn setImage:[UIImage imageNamed:@"task_delete1"] forState:UIControlStateNormal];
    [self addSubview:removeBtn];
    [removeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(15));
        make.width.height.offset(kwt(20));
        make.left.offset(kwt(10));
    }];
    
    UILabel * sourceFlagLab = [DTKTool labelWithText:@"-" andFont:kFont(19) andTextColor:KcolorHex(@"#222222")];
    self.sourceFlagLab = sourceFlagLab;
    [self addSubview:sourceFlagLab];
    [sourceFlagLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(removeBtn);
        make.left.offset(kwt(36));
    }];
    
    
    
    UIView * qztsView = [UIView new];
    qztsView.clipsToBounds = YES;
    self.qztsView = qztsView;
    [self addSubview:qztsView];
    [qztsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(45));
        make.left.right.offset(0);
        make.height.offset(kwt(63));
    }];
    
    UIView * qztsbackView = [UIView new];
    qztsbackView.backgroundColor = KcolorHex(@"#F6F7F9");
    [DTKTool YJ:kwt(5) andView:qztsbackView];
    [qztsView addSubview:qztsbackView];
    [qztsbackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.top.offset(0);
        make.right.bottom.offset(kwt(-10));
    }];
    
    UILabel * temp1 = [UILabel new];
    temp1.attributedText = [DTKTool AttributedStringWithStr:@"*起止天数" andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kCFont(17) andPattenr:@"[*]"];
    [qztsbackView addSubview:temp1];
    [temp1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.centerY.offset(0);
    }];
    
    
    UITextField * startDay = [UITextField new];
    startDay.textAlignment = NSTextAlignmentCenter;
    startDay.delegate = self;
    startDay.keyboardType = UIKeyboardTypeNumberPad;
    self.startDay = startDay;
    [qztsbackView addSubview:startDay];
    startDay.backgroundColor = UIColor.whiteColor;
    [DTKTool YJBK:kwt(4) andView:startDay andWidth:1 andColor:KcolorHex(@"#D9D9D9")];
    [startDay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(kwt(77));
        make.left.offset(kwt(132));
        make.height.offset(kwt(32));
        make.centerY.offset(0);
    }];
//    [startDay addTarget:self action:@selector(textFieldDidChangeValue:)  forControlEvents:UIControlEventEditingChanged];

    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#B1B1B1");
    [qztsbackView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(kwt(14));
        make.left.equalTo(startDay.mas_right).offset(kwt(10));
        make.height.offset(kwt(2));
        make.centerY.offset(0);
    }];
    
    UITextField * endDay = [UITextField new];
    endDay.delegate = self;
    endDay.keyboardType = UIKeyboardTypeNumberPad;
    endDay.textAlignment = NSTextAlignmentCenter;
    self.endDay = endDay;
    [qztsbackView addSubview:endDay];
    endDay.backgroundColor = UIColor.whiteColor;
    [DTKTool YJBK:kwt(4) andView:endDay andWidth:1 andColor:KcolorHex(@"#D9D9D9")];
    [endDay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(kwt(77));
        make.left.equalTo(line.mas_right).offset(kwt(10));
        make.height.offset(kwt(32));
        make.centerY.offset(0);
    }];
//    [endDay addTarget:self action:@selector(textFieldDidChangeValue:)  forControlEvents:UIControlEventEditingChanged];

    UILabel * temp2 = [DTKTool labelWithText:@"天" andFont:kFont(17) andTextColor:KcolorHex(@"#222222")];
    [qztsbackView addSubview:temp2];
    [temp2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(endDay.mas_right).offset(kwt(6));
        make.centerY.offset(0);
    }];
    
    
    UIView * rwpcView = [UIView new];
    rwpcView.clipsToBounds = YES;
    self.rwpcView = rwpcView;
    [self addSubview:rwpcView];
    [rwpcView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(qztsView.mas_bottom);
        make.left.right.offset(0);
        make.height.offset(kwt(63));
    }];
  
    UIView * rwpcbackView = [UIView new];
    rwpcView.addClickBlock(^{
        [weakSelf openRWPC];
    });
    rwpcbackView.backgroundColor = KcolorHex(@"#F6F7F9");
    [DTKTool YJ:kwt(5) andView:rwpcbackView];
    [rwpcView addSubview:rwpcbackView];
    [rwpcbackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.top.offset(0);
        make.right.bottom.offset(kwt(-10));
    }];
    
    UILabel * temp3 = [UILabel new];
    temp3.attributedText = [DTKTool AttributedStringWithStr:@"*任务频次" andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kCFont(17) andPattenr:@"[*]"];
    [rwpcbackView addSubview:temp3];
    [temp3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.centerY.offset(0);
    }];
    
    UIImageView * img1 = kimageView(@"template_enter");
    [rwpcbackView addSubview:img1];
    [img1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-12));
        make.width.height.offset(kwt(15));
        make.centerY.offset(0);
    }];
    UILabel * rateLab = [DTKTool labelWithText:@"--" andFont:kFont(18) andTextColor:KcolorHex(@"#222222")];
    self.rateLab =rateLab;
    rateLab.textAlignment = NSTextAlignmentRight;
    [rwpcbackView addSubview:rateLab];
    [rateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-35));
        make.left.equalTo(temp3.mas_right).offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    
    
    UIView * rwsjView = [UIView new];
    rwsjView.clipsToBounds = YES;
    self.rwsjView = rwsjView;
    [self addSubview:rwsjView];
    [rwsjView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(rwpcView.mas_bottom);
        make.left.right.bottom.offset(0);
    }];
  
    UIView * rwsjbackView = [UIView new];
    rwsjbackView.backgroundColor = KcolorHex(@"#F6F7F9");
    [DTKTool YJ:kwt(5) andView:rwsjbackView];
    [rwsjView addSubview:rwsjbackView];
    [rwsjbackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.top.offset(0);
        make.right.bottom.offset(kwt(-10));
    }];
    
    UILabel * temp4 = [UILabel new];
    temp4.attributedText = [DTKTool AttributedStringWithStr:@"*任务执行时间" andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kCFont(17) andPattenr:@"[*]"];
    [rwsjbackView addSubview:temp4];
    [temp4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.offset(kwt(18));
    }];
    
    UIView * rwTimeView = [UIView new];
    self.rwTimeView = rwTimeView;
    [rwsjbackView addSubview:rwTimeView];
    [rwTimeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.top.offset(kwt(45));
        make.bottom.offset(kwt(-10));
    }];
    
}
- (void)scbtnClick{
    if(self.openRemoveBlock){
        self.openRemoveBlock(self.model);
    }
}
- (void)openSXBtnClick{
    self.model.isSQ = !self.model.isSQ;
    if(self.openReloadBlock){
        self.openReloadBlock();
    }
}


- (void)openRWPC{
    if(self.openRWPCBlock){
        self.openRWPCBlock(self.model);
    }
}
- (void)setIndex:(int)i andSourceFlagName:(NSAttributedString *)name {
    self.numberLab.text = [NSString stringWithFormat:@"%d",i];
    self.sourceFlagLab.attributedText = name;
}
- (void)setModel:(TemplateFoundDetailsTaskTypeModelListModel *)model{
    _model = model;
    if(model.sourceFlag == 0){///新增的
        self.nameLab.hidden = YES;
        self.numberLab.hidden = YES;
        self.removeBtn.hidden = NO;
        self.sourceFlagLab.hidden = NO;
    } else {
        self.nameLab.hidden = NO;
        self.numberLab.hidden = NO;
        self.removeBtn.hidden = YES;
        self.sourceFlagLab.hidden = YES;

    }
    self.nameLab.text = model.name;
//    self.sourceFlagLab.text = model.sortName;

    ///executeType 执行天数类型:1:自定义天数 2:整个养殖周期
    self.endDay.text = [NSString stringWithFormat:@"%ld",(long)model.endDay];
    self.startDay.text = [NSString stringWithFormat:@"%ld",(long)model.startDay];
    if(model.taskType == 2){///自定义
        self.rateLab.text = @"自定设置";
        [self.qztsView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(0);
        }];
    } else {
        self.rateLab.text = [NSString stringWithFormat:@"%ld天%ld次",model.rateDay,model.rateNumber];
        [self.qztsView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset((model.executeType == 2)?0:kwt(63));
        }];
    }
    
    self.sxBtn.selected = model.isSQ;
    [self.rwTimeView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
 
    CGFloat X = 0;
    CGFloat Y = 0;
    MJWeakSelf
    for (TemplateFoundDetailsExecuteModelListModel * timeModel in model.executeModelList) {//步骤
        UIView * btnview = [UIView new];
        btnview.addClickBlock(^{
            if(weakSelf.openRWTimeBlock){
                weakSelf.openRWTimeBlock(timeModel);
            }
        });
        btnview.backgroundColor = UIColor.whiteColor;
        [DTKTool YJBK:kwt(3) andView:btnview andWidth:1 andColor:KcolorHex(@"#E2E2E2")];
        if(timeModel.executeTime.length > 0){//有时间
            UILabel * leftlab = [DTKTool labelWithText:timeModel.executeTime andFont:kFont(18) andTextColor:KcolorHex(@"#222222")];
            leftlab.textAlignment = NSTextAlignmentCenter;
            [btnview addSubview:leftlab];
            [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.offset(0);
            }];
        } else {//没有时间
            UILabel * leftlab = [DTKTool labelWithText:@"选择\n时间" andFont:kFont(14) andTextColor:KcolorHex(@"#222222")];
            leftlab.numberOfLines = 0;
            [btnview addSubview:leftlab];
            [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(kwt(11));
                make.centerY.offset(0);
            }];
            UIImageView * rightimg = kimageView(@"template_enter");
            [btnview addSubview:rightimg];
            [rightimg mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.offset(kwt(-11));
                make.width.height.offset(kwt(15));
                make.centerY.offset(0);
            }];
        }
        btnview.frame = CGRectMake(X, Y, kwt(78), kwt(48));
        [self.rwTimeView addSubview:btnview];
        X += kwt(85);
        if(X >kwt(320)){
            X = 0;
            Y += kwt(53);
        }
    }
    
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"编辑结束");
    if(textField.text.length < 1){
            textField.text = @"0";
    }
    
    textField.text = [NSString stringWithFormat:@"%ld",textField.text.integerValue];
    if(self.endDay == textField){//结束
        if(textField.text.integerValue < self.model.startDay){
            self.model.endDay = self.model.startDay;
            textField.text = [NSString stringWithFormat:@"%ld",(long)self.model.endDay];
//                        if(self.openHUDBlock){
//                            self.openHUDBlock(@"不能比开始时间早");
//                        }
        } else {
            self.model.endDay = textField.text.integerValue;
        }
        if(self.breedCycle.length > 0){
            if(textField.text.integerValue > self.breedCycle.intValue){
                if(self.openHUDBlock){
                    self.openHUDBlock(@"结束天数不可大于养殖周期");
                }
                textField.text = self.breedCycle;
                self.model.endDay = textField.text.integerValue;
            }
        }
    } else if(self.startDay == textField){//开始
        if(textField.text.integerValue > self.model.endDay){
            self.model.startDay = self.model.endDay;
            textField.text = [NSString stringWithFormat:@"%ld",(long)self.model.startDay];
//                        if(self.openHUDBlock){
//                            self.openHUDBlock(@"不能比结束时间晚");
//                        }
        } else {
            self.model.startDay = textField.text.integerValue;
        }
    }
    
}
//- (void)textFieldDidChangeValue:(UITextField *)sender{
//    if(sender.text.length < 1){
//        sender.text = @"0";
//    }
//    sender.text = [NSString stringWithFormat:@"%ld",sender.text.integerValue];
//    if(self.endDay == sender){//结束
//        if(sender.text.integerValue < self.model.startDay){
//            self.model.endDay = self.model.startDay;
//            sender.text = [NSString stringWithFormat:@"%ld",(long)self.model.endDay];
////            if(self.openHUDBlock){
////                self.openHUDBlock(@"不能比开始时间早");
////            }
//        } else {
//            self.model.endDay = sender.text.integerValue;
//        }
//    } else if(self.startDay == sender){//开始
//        if(sender.text.integerValue > self.model.endDay){
//            self.model.startDay = self.model.endDay;
//            sender.text = [NSString stringWithFormat:@"%ld",(long)self.model.startDay];
////            if(self.openHUDBlock){
////                self.openHUDBlock(@"不能比结束时间晚");
////            }
//        } else {
//            self.model.startDay = sender.text.integerValue;
//        }
//    }
//    
//    
//}
@end
